<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$act = $_POST['act'];
	$bill = $_POST['bill'];
	$idfront_office = $_POST['idfront_office'];
	$idresto = $_POST['idresto'];
	$no_voucher = $_POST['no_voucher'];
	$payment = $_POST['payment'];
	$total = $_POST['total'];

	switch ($act) {
		case '1':
			if ($payment == 4) {
				$is_service_charge = 1;
				$payment = 0;

				$sql_resto_menu = "SELECT resto_menu.*, menu.nama AS nama_menu 
													FROM resto_menu 
													INNER JOIN menu 
														ON menu.id = resto_menu.idmenu
													WHERE resto_menu.idresto = '$idresto' AND resto_menu.bill = '$bill'";
				$query_resto_menu = mysql_query($sql_resto_menu);
				while ($resto_menu = mysql_fetch_array($query_resto_menu)) {
					$sql_check = "SELECT * FROM service_charge WHERE idfront_office = '$idfront_office' AND idmenu = '$resto_menu[idmenu]'";
					$query_check = mysql_query($sql_check);
					if (mysql_num_rows($query_check) > 0) {
						$query_check = mysql_query($sql_check);
						$check = mysql_fetch_array($query_check);
						$jumlah = $check[jumlah] + $resto_menu[jumlah];

						$sql_update = "UPDATE service_charge SET jumlah = '$jumlah' WHERE id = '$check[id]'";
						$query_update = mysql_query($sql_update);
					} else {
						$sql_insert_service_charge = "INSERT INTO service_charge (idfront_office, idmenu, nama, jumlah, harga) VALUES ('$idfront_office', '$resto_menu[idmenu]', '$resto_menu[nama_menu]', $resto_menu[jumlah], $resto_menu[harga])";
						$query_insert_service_charge = mysql_query($sql_insert_service_charge);
					}
				}
			} else {
				$is_service_charge = 0;
			}

			$sql_insert = "INSERT INTO nota (idresto, bill, total_bill, payment, no_voucher, is_paid, is_service_charge) VALUES ('$idresto', '$bill', '$total', '$payment', '$no_voucher', '1', '$is_service_charge')";
			$query_insert = mysql_query($sql_insert);

			echo "sukses";
			break;

		case '2':
			if ($bill != 1) {
				echo "sukses";
			} else {
				$sql = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill NOT IN (SELECT bill FROM nota WHERE idresto = '$idresto' AND is_paid = '1') GROUP BY bill";
				$query = mysql_query($sql);
				if (mysql_num_rows($query) == 1) {
					echo "sukses";
				}
			}
			break;

		default:
			
			break;
	}
?>